package com.example.myapplication.fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.R;
import com.example.myapplication.adapter.MessageListAdapter;
import com.example.myapplication.bean.MessageListBean;
import com.example.myapplication.utli.UtilsEnum;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment4#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment4 extends Fragment {
    ListView mLvMessage;
    RequestQueue queue;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Fragment4() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment4.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment4 newInstance(String param1, String param2) {
        Fragment4 fragment = new Fragment4();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_4, container, false);
        getMessage(view);

        EditText etMessage = view.findViewById(R.id.et_message);
        Button btnSend = view.findViewById(R.id.btn_send_meesage);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = etMessage.getText() + "";
                JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
                        UtilsEnum.URL.url + "messages/add?content="+message+"&date="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
                                +"&userid="+UtilsEnum.ID, new JSONObject()
                        , res -> {
                    getMessage( view);
                }, error -> {
                });
                queue.add(request);
            }
        });
        return view;
    }

    public void getMessage(View view) {
        mLvMessage =  view.findViewById(R.id.lv_message);
        // 创建请求队列
        queue = Volley.newRequestQueue(getContext());
        // 创建请求
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, UtilsEnum.URL.url + "messages/list",
                new JSONObject(),(res-> {
            Gson gson = new Gson();
            MessageListBean messageListBean = gson.fromJson(res.toString(), MessageListBean.class);
            MessageListAdapter messageListAdapter = new MessageListAdapter(messageListBean.getData(),getContext());
            mLvMessage.setAdapter(messageListAdapter);
        }),(error -> {

        }));

        // 将请求加入队列
        queue.add(request);
    }
}